<?php
/**
 * @link https://www.kancloud.cn/cleverstone/ymb2
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\uploads;

use Yii;
use yii\base\InvalidArgumentException;
use yii\helpers\FileHelper;

/**
 * 文件上传本地
 * ```example
 * //main.php中组件定义
 * 'uploadManager' => [
 *      'class' => 'builder\uploads\Upload',
 * ],
 * ```
 * @author clevstn
 * @since ym2.0
 */
class LocalProvider extends BaseProvider
{
    //值为`@web`或`@web/A/B`
    //public $web;
    //值为`@webroot`或`@webroot/A/B`
    //public $webroot;
    /**
     * 上传入口
     * @param bool $deleteTempFile
     * @return bool
     */
    protected function run($deleteTempFile = true)
    {
        list($basename, $saveFilePath) = $this->generateUniqueFilePath();
        $this->link = $this->web . '/' . $basename;

        if (is_file($saveFilePath)) {//文件已存在
            return true;
        }

        if (!$this->uploadFile->saveAs($saveFilePath, $deleteTempFile)) {
            $this->error = 'File upload failure!';
            return false;
        }

        return true;
    }
}